/*
 * @Author: 李一番
 * @Date: 2021-10-26 16:44:48
 * @Last Modified by: 李一番
 * @Last Modified time: 2021-12-28 14:15:15
 */

import Cookie from 'js-cookie'
import { RouteLocationNormalized } from 'vue-router'

interface BasicStore {
  token: string | number | undefined // token
  userInfo: object | undefined // 用户详情
  language: string | undefined // 语言
  menuPermission: string | undefined // 菜单权限
  buttonPermission: string | undefined // 按钮权限
}

export type BasicKeys = keyof BasicStore

// 获取cookie
export function getCookie(name: BasicKeys) {
  return Cookie.get(name)
}
// 设置cookie
export function setCookie(name: BasicKeys, value: string) {
  return Cookie.set(name, value)
}

// 删除cookie
export function removeCookie(name: BasicKeys) {
  return Cookie.remove(name)
}

// 清除所有的cookies
export function removeAllCookies() {
  removeCookie('token')
  removeCookie('userInfo')
  removeCookie('language')
  removeCookie('menuPermission')
  removeCookie('buttonPermission')
}

// 判断是否有菜单权限
export function hasMenuPermission(permission: any, route: RouteLocationNormalized) {
  if (route.meta && route.meta.api) {
    return `${permission[(route.meta.api as string)]}` === '1';
  } else {
    return true;
  }
}

// 判断是否具有按钮权限
export function hasBtnPermission(btnName: string) {
  const permis = getCookie('buttonPermission');
  return permis && permis.includes(btnName);
}
